package org.gwt.grin.client.util;

import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.FontStyle;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Widget;

/**
 * This is a data holder for the specification of a font, consisting of a name,
 * size and style.
 * 
 * @author ggeorg
 */
public class FontSpec {

	private static final String FONT_FAMILY = "fontFamily";

	private final String name;
	private final FontStyle style;
	private final FontWeight weight;
	private final int size;

	public FontSpec(String name, FontStyle style, FontWeight weight, int size) {
		this.name = name;
		this.style = style;
		this.weight = weight;
		this.size = size;
	}

	public String getName() {
		return name;
	}

	public FontStyle getStyle() {
		return style;
	}

	public FontWeight getWeight() {
		return weight;
	}

	public int getSize() {
		return size;
	}

	public void applyTo(Widget w) {
		Style s = w.getElement().getStyle();
		s.setProperty(FONT_FAMILY, name);
		s.setFontWeight(weight);
		s.setFontStyle(style);
		s.setFontSize(size, Unit.PX);
	}

	@Override
	public boolean equals(Object other) {
		if (other instanceof FontSpec) {
			FontSpec fs = (FontSpec) other;
			return name.equals(fs.name) && style == fs.style
					&& weight == fs.weight && size == fs.size;
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + name.hashCode();
		result = prime * result + style.hashCode();
		result = prime * result + weight.hashCode();
		result = prime * result + size;
		return result;
	}
}
